package czy.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;


@RestController
@Api(tags = "会话控制器")
public class SessionController {

    @GetMapping("/sessionid")
    @ApiOperation("获取会话ID")
    public String session(
           @ApiIgnore HttpSession session
    ){
        return session.getId();
    }

    @PostMapping("/set")
    @ApiOperation("设置会话属性")
    public String setUsername(
            @ApiIgnore HttpSession session,
            @ApiParam("属性名") @RequestParam String name,
            @ApiParam("属性值") @RequestParam String value
    ){
        session.setAttribute(name,value);
        return "设置成功";
    }

    @PostMapping("/get")
    @ApiOperation("获取会话属性")
    public String getUsername(
            @ApiIgnore HttpSession session,
            @ApiParam("属性名") @RequestParam String name
    ){
        return (String)session.getAttribute(name);
    }

}
